package com.wuseguang.springcloud.util.MyControllerAdvice;

import com.wuseguang.springcloud.JdbcTemplate.BaseTemplate;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @描述 捕获异常，处理
 * @创建人 zm
 * @创建时间 2020/5/27
 */
@ControllerAdvice
public class MyControllerAdvice {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 设置要捕获的异常，并作出处理
     * 注意：这里可以返回试图，也可以放回JSON，这里就当做一个Controller使用
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result processUnauthenticatedException(HttpServletRequest request, Exception e) {
        logger.info("全局捕获异常：url={}", request.getRequestURL());
        Map map = request.getParameterMap();
        Set<String> keySet = map.keySet();
        Iterator<String> it = keySet.iterator();
        while (it.hasNext()) {
            String key = it.next();
            Object o = map.get(key);
            String value[] = (String[]) o;
            logger.info("参数：name={}, 传值：value={}", key, value[0]);
        }
        logger.info("全局捕获异常:error={}", e);
        return new Result(CodeUtil.SERVER_BUSY);
    }
}
